package cn.dragon.eatingfish;

import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class TipMusicPlayer implements Runnable {
    private String music;        // 音乐路径，定义播放哪首音乐
    private static boolean running = true;

    public TipMusicPlayer(String music) {
        this.music = music;
    }

    @Override
    public void run() {
        File file = new File(music);
        try {
            Player player = new Player(new FileInputStream(file));
            player.play();
            if (player.isComplete()) {
                this.running = false;
            }
        } catch (JavaLayerException | FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public String getMusic() {
        return music;
    }

    public void setMusic(String music) {
        this.music = music;
    }

    public boolean isRunning() {
        return running;
    }

    // 播放吃鱼提示音
    public static void playTipMusic(String music) {
        Runnable runnable = new TipMusicPlayer(music);
        //将Runnable接口的子类对象作为参数传递给Thread类的构造函数
        Thread thread = new Thread(runnable);
        //开启线程
        thread.start();
    }
}
